<?php if ($show_rating == 1){?>
<script src="../js/rating/jquery.MetaData.js" type="text/javascript"></script>
<script src="../js/rating/jquery.rating.js" type="text/javascript"></script>

<link href="../css/rating/jquery.rating.css" type="text/css" rel="stylesheet">
<?php }?>


<div id="comments_list">
	<div style="clear:both;margin-bottom: 15px;margin-top: 15px;"><h2><?php echo $template->getCommentHeader()?></h2></div>

<?php foreach ($comments as $comment){
	?>
	<div style="clear:both">
		<div style="float:left;">
		<?php 
		$comment_date_arr = explode("-", $comment->getDate()) ;
		
		
		$comment_day = explode(" ", $comment_date_arr[2]);
		
		?>
		Posted by <?php echo $comment->getUserName()?> on <?php echo date("F d, Y", mktime(0, 0, 0, $comment_date_arr[1], $comment_day[0], $comment_date_arr[0])) ?>
		</div>	
		
		<?php if ($show_rating == 1){?>
		<div style="margin-left:40px; float:left;" id="rated_div_<?php echo $comment->getId()?>"><?php echo include_partial("comments/rate", array('comment_id' => $comment->getId(), 'rate' => $comment->getAverageRating()))?></div>
		<?php }?>
	</div>
	<?php
	$comment_values = $comment->getCommentFieldValue();
	foreach ($comment_values as $value){
		if($value->getValue() != ""){
		
		if ($value->getCommentField()->getFieldTypeId() == sfConfig::get("app_field_type_text_area") ){?>
			<div style="clear:both; padding:10px;">
			<div><b><?php echo $value->getCommentField()?>:</b></div>
			<div><?php echo $value->getValue()?></div>
			</div>
		<?php }else if ($value->getCommentField()->getFieldTypeId() == sfConfig::get("app_field_type_text")){?>			
			 <div style="clear:both; padding:10px;"><b><?php echo $value->getCommentField()?>:</b> <?php echo $value->getValue()?></div>
		<?php }else if ($value->getCommentField()->getFieldTypeId() == sfConfig::get("app_field_type_video")){?>
			<div style="clear:both; padding:10px;"><iframe width="453" height="280" src="<?php echo $value->getValue()?>" frameborder="0" ></iframe></div>
		<?php }else if ($value->getCommentField()->getFieldTypeId() == sfConfig::get("app_field_type_image")){?>
			<div style="clear:both; padding:10px;"><img alt="" src="<?php echo $value->getValue()?>" style="max-width:453px"></div>
		<?php }?>
		
	<?php 
		}
	}
	?>

<?php 
if ($show_rating == 1){
$rated_comments_ids = array();
if ($sf_user->getAttribute("comments") != null){
	foreach ($sf_user->getAttribute("comments") as $comment_id){
		array_push($rated_comments_ids, $comment_id);
	
	}
}
?>
<?php if ( !in_array($comment->getId(), $rated_comments_ids)){?>
	<div style="clear:both;margin-top: 10px;" id="rating_div_<?php echo $comment->getId()?>">
			<div><?php echo $template->getRateTxt()?></div>
				<input name="star_<?php echo $comment->getId()?>" value="1" type="radio" class="auto-submit-star"/>
				<input name="star_<?php echo $comment->getId()?>" value="2" type="radio" class="auto-submit-star"/>
				<input name="star_<?php echo $comment->getId()?>" value="3" type="radio" class="auto-submit-star"/>
				<input name="star_<?php echo $comment->getId()?>" value="4" type="radio" class="auto-submit-star"/>
				<input name="star_<?php echo $comment->getId()?>" value="5" type="radio" class="auto-submit-star"/>
	</div>	
<?php 
}

}?>	
		
	<div class="slider_divider" style="border-bottom-color:#C0C0C0; margin-top:30px;"></div>
	<div style="clear:both;margin-top: 15px;"></div>
<?php }?>

</div>   
<?php if(count($template->getCommentField()) > 0 && $template->getCommentButtonCaption() != '' && $template->getCommentHeader() != '') { ?>
<div style="border: 1px solid #ECECEC; margin-bottom: 20px;  margin-top: 20px;  padding: 10px;">
<div style="clear:both"></div>

<?php if($template->getCommentLoginDescription() != ''){?>
<div style="clear:both;padding: 15px;">
<h6 style="font-size: 16px;line-height: 25px;">
<?php echo $template->getCommentLoginDescription()?>
</h6>
</div>
<?php }?>


<form id="comments_form" action="<?php echo url_for("comments/saveComment")?>" method="post">

<input type="hidden" name="my_current_url" value="" id="my_current_url" />
<?php 
	foreach ($comment_fields as $comment_field){
		?>
		<div style="clear:both; width:350px;margin-top:10px;">
			<div style="float:left; width: 120px; text-align: right;padding-top: 5px;">
			<?php 
			echo $comment_field->getName();
			echo $comment_field->getRequired() ? '*' : '';
			?></div>
			<div style="float:right;width: 220px;">
				<?php if ($comment_field->getFieldTypeId() == sfConfig::get("app_field_type_text_area")){
					?>
					<div style="font-size:11px;text-align: right; width: 334px;">You have <div style="display:inline; font-weight:bold" id="countdown">255</div> characters left.
					</div>					
				<textarea onKeyDown="limitText($('#comment_field_id_<?php echo $comment_field->getId()?>'), $('#countdown'), 255);" onKeyUp="limitText($('#comment_field_id_<?php echo $comment_field->getId()?>'),$('#countdown'),255);" rows="5" cols="80" style="width:325px" name="comment_field_id_<?php echo $comment_field->getId()?>" id="comment_field_id_<?php echo $comment_field->getId()?>"></textarea>

					<?php } else if ($comment_field->getFieldTypeId() != sfConfig::get("app_field_type_image") && $comment_field->getFieldTypeId() != sfConfig::get("app_field_type_video")) {?>
						<input maxlength="46" type="text" name="comment_field_id_<?php echo $comment_field->getId()?>" id="comment_field_id_<?php echo $comment_field->getId()?>" style="width:325px;">
				
				<?php }else{
					?>
						<input type="text" name="comment_field_id_<?php echo $comment_field->getId()?>" id="comment_field_id_<?php echo $comment_field->getId()?>" style="width:325px;">
					<?php
				}
				?>
			</div>

			<div style="clear:both"></div>
			
		</div>
		
		<?php
	
	}
?>
<div id="error_msg2" class="error_msg" style="display:none; clear: both;cursor: pointer;margin-left: 132px; text-align: center; width: 319px;" onclick="$(this).hide('slow')">
	<div id="msg_text2" style="text-align: center" ></div>
</div>	



<div style="clear:both; width:465px;margin-top:10px;text-align: right;" >
<!-- <a id="contact_form_formsend" class="button" href="#"><?php //echo $template->getCommentButtonCaption()?></a> -->	

	<div id="contact_form_formsend" style="display:none;float:right;">
	<!-- 	<input type="button" onclick="postonwall();docomment();" class="button" value="publish" scope="publish_stream"> -->
		<input type="button" onclick="postonwall();" class="button" value="publish" scope="publish_stream">
	</div>	
	
	<div id="fb_login_button" style="float: right; width: 70px;">
		<div class="fb-login-button" data-show-faces="false" data-width="200" data-max-rows="1" scope="publish_stream" ></div>
	</div>
</div>

<input type="hidden" name="template_id" value="<?php echo $template->getId()?>">

<?php 
$category_content_ids_str = "";
foreach ($category_content_ids as $category_content_id){
		$category_content_ids_str .= $category_content_id . '-';
}
$category_content_ids_str = substr($category_content_ids_str, 0, strlen($category_content_ids_str) - 1);
?>
<input type="hidden" name="category_content_ids" value="<?php echo $category_content_ids_str?>">

<input type="hidden" name="h_fb_user" id="h_fb_user" value="">


</form>
<div style="clear:both"></div>
</div>   
  
  <?php }?>    
<script>

function validateCommentsForm(){
	var error_msg = "";
	var valid = true;
	<?php 
	foreach ($comment_fields as $comment_field){
		if ($comment_field->getRequired()){
	?>
	if ($('#comment_field_id_<?php echo $comment_field->getId()?>').attr('value') == ""){
		error_msg = "<?php echo __("Required field") . ': ' . $comment_field->getName() ?>";
		valid = false;
	}

	<?php
	if ($comment_field->getFieldTypeId() == sfConfig::get("app_field_type_video") || $comment_field->getFieldTypeId() == sfConfig::get("app_field_type_image")){?>
	var url = $('#comment_field_id_<?php echo $comment_field->getId()?>').attr('value');

	if (valid){
		if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url)){
		}
		else{
			error_msg = "<?php echo $comment_field->getName() . ": " . __("Invalid url")?>";
			valid = false;
		}
	}
	<?php 
	}
	}else if ($comment_field->getFieldTypeId() == sfConfig::get("app_field_type_video") || $comment_field->getFieldTypeId() == sfConfig::get("app_field_type_image")){
		?>
		if ($('#comment_field_id_<?php echo $comment_field->getId()?>').attr('value') != ""){
			if (valid){
				var url = $('#comment_field_id_<?php echo $comment_field->getId()?>').attr('value');
				if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(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|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url)){
				}
				else{
					error_msg = "<?php echo $comment_field->getName() . ": " . __("Invalid url")?>";
					valid = false;
				}
			}
		}
		<?php 
	}
	}?>

	if (error_msg != ""){
		$('#msg_text2').html(error_msg);
		$('#error_msg2').show('fast');
	}
	return valid;
	
}

function docomment(){
	if (!validateCommentsForm()){
		return false;
	}
	else{
		
		
		$('#my_current_url').attr('value', $(location).attr('href'));

		$('a#contact_form_formsend').text('<?php echo __("Sending comment")?>...');

		
		//event.preventDefault();

		  
		$('form:#comments_form').submit();
	}
}

/*
$("#contact_form_formsend").click(function (event){
	if (!validateCommentsForm()){
		return false;
	}
	else{
		
		
		$('#my_current_url').attr('value', $(location).attr('href'));

		$('a#contact_form_formsend').text('<?php //echo __("Sending comment")?>...');

		
		event.preventDefault();

		  
		$('form:#comments_form').submit();
	}
             
})

*/



function limitText(limitField, limitCount, limitNum) {
	if ($(limitField).attr("value").length > limitNum) {
		$(limitField).attr("value",$(limitField).attr("value").substring(0, limitNum));
	} else {
		$(limitCount).html(limitNum - $(limitField).attr("value").length);
	}
}


function postonwall()
{

	var the_message = "";
	
	var the_link = $(location).attr('href');
	var post_title = '<?php echo $directory->getDomain()?>';
	var the_picture = '';
	var the_description = "The description";
	
	<?php 
	foreach ($comment_fields as $comment_field){
		
		if ($comment_field->getFieldTypeId() == sfConfig::get("app_field_type_image")){
			?>
			the_picture = $('#comment_field_id_<?php echo $comment_field->getId()?>').attr('value');
			<?php 
			
		}
		else if ($comment_field->getFieldTypeId() != sfConfig::get("app_field_type_video")){
	?>
		
		the_message += "<?php echo $comment_field->getName()?>" + ": " + " " + $('#comment_field_id_<?php echo $comment_field->getId()?>').attr('value') + "\n";

		
	<?php 
	}
	}
	?>

		  
	FB.api('/me/feed', 'post',
    {
   
        link: the_link,
        name: post_title,
        picture: the_picture,        
        description: the_message
    }, function(response) {
        if (!response || response.error) {
            alert('Oops! User Denied Access');
        } else {
            alert('Success: Content Published');
            docomment();
        }
    }); 

	/*
	FB.api('/me/feed', 'post',
	{
        message: 'Testing posting from my app on FB',
        link:'http://couturein.com',
        name:'Post title',
        picture:'http://www.demo.lookmywebpage.com/publish-on-facebook-wall/Google-Twitter-Facebook.jpg',
        
        description:'Know how to work with facebook, twitter and various other APIs. Demo and source code available for key topics'
    }, function(response) {
        if (!response || response.error) {
            alert('Oops! User Denied Access');
        } else {
            alert('Success: Content Published');
        }
    });
*/
}

$('.auto-submit-star').rating({
	callback: function(value, link){
	//alert(value);
		var html_radio = $(this)[0];
		
			var html_radio_name = $(html_radio).attr("name");

			var html_radio_name_arr = html_radio_name.split("_");

			var comment_id = html_radio_name_arr[1];
		
	 	   var refresh =  "rating_div_" + comment_id;

	 	   var rated_div = "#rated_div_" + comment_id;

		   var str="comment_id=" + comment_id + "&rating=" + value;
		 	  
		    $.ajax({type: 'POST', url:' <?php echo url_for('comments/rate')?>',  script:true, cache:false, data:  str,

	       success: function(data) {
	           var div_to_refresh = document.getElementById(refresh);
	            $(div_to_refresh).hide();
	            
	            $(rated_div).html(data);
	          	 
	        } });		
		
	}
});

/*
$("#stars-wrapper1").stars({
    cancelShow: false
});
*/

//$('input').rating('required', false);

	
</script>      
<!--    
<div class="fb-login-button" scope="email,user_checkins">
   Login with Facebook
</div>
 -->      
      